<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import api from "@/utils/api"
import { saveJwt } from "@/api/jwt.js"

const router = useRouter()
const loading = ref(false)
const loginFormRef = ref()

// 登录表单数据
const loginFormModel = reactive({
  phone: '',
  password: ''
})

// 表单验证规则
const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ]
}

// 提交登录
const handleLogin = async () => {
  if (loading.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    // 调用实际的登录API
    const response = await api.post('/login', {
      phone: loginFormModel.phone,
      password: loginFormModel.password
    })
    if (response.code === 200 && response.success) {
      // 保存JWT token (token直接在data字段中)
      saveJwt(response.data)
      ElMessage.success("登录成功")
      // 登录成功后跳转到主页
      router.push('/main')
    } else {
      ElMessage.error(response.message || "登录失败")
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error("登录过程中发生错误，请稍后重试")
  } finally {
    loading.value = false
  }
}

// 重置表单
const resetForm = () => {
  loginFormRef.value.resetFields()
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<template>
  <div class="main">
    <div class="login-container">
      <div class="login-card">
        <div class="login-left">
          <div class="pic"></div>
        </div>
        <div class="login-right">
          <div class="form">
            <h1 class="title">宙斯健身馆</h1>
            <el-form
                class="login-form"
                :model="loginFormModel"
                :rules="rules"
                ref="loginFormRef"
                @keyup.enter="handleLogin"
            >
              <el-form-item prop="phone">
                <el-input
                    v-model="loginFormModel.phone"
                    placeholder="请输入手机号"
                    prefix-icon="Phone"
                    size="large"
                    clearable
                    autocomplete="username"
                />
              </el-form-item>

              <el-form-item prop="password">
                <el-input
                    type="password"
                    v-model="loginFormModel.password"
                    placeholder="请输入密码"
                    prefix-icon="Lock"
                    show-password
                    size="large"
                    autocomplete="current-password"
                />
              </el-form-item>

              <el-form-item>
                <el-button
                    type="primary"
                    class="login-btn"
                    @click="handleLogin"
                    :loading="loading"
                    size="large"
                    round
                    block
                >
                  {{ loading ? '登录中...' : '登 录' }}
                </el-button>
              </el-form-item>

              <el-form-item>
                <el-button
                    class="reset-button"
                    @click="resetForm"
                    size="large"
                    round
                    block
                >
                  重置
                </el-button>
              </el-form-item>

              <el-form-item>
                <el-button
                    class="register-button"
                    @click="goToRegister"
                    size="large"
                    round
                    block
                >
                  注册账号
                </el-button>
              </el-form-item>

            </el-form>
          </div>
        </div>
      </div>
    </div>

    <div class="copyright">
      <p>&copy; 2025 宙斯健身馆 版权所有</p>
    </div>
  </div>
</template>

<style scoped>
.main {
  height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
  url("@/assets/loginBackground.png") no-repeat center center/cover;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  flex: 1;
}

.login-card {
  display: flex;
  width: 900px;
  height: 500px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  backdrop-filter: blur(10px);
}

.login-left {
  flex: 1;
  background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
  url("@/assets/loginBody.jpg") no-repeat center center/cover;
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #1e3c72;
  font-size: 28px;
  font-weight: bold;
}

.login-form {
  width: 100%;
}

.login-btn {
  margin-top: 10px;
  font-weight: bold;
  letter-spacing: 2px;
}

.reset-button {
  margin-top: 10px;
  font-weight: bold;
  letter-spacing: 2px;
  background: transparent;
  border: 2px solid #409eff;
  color: #409eff;
}

.copyright {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
}

.copyright p {
  margin: 0;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    width: 90%;
    flex-direction: column;
    height: auto;
  }

  .login-left {
    height: 200px;
  }

  .login-right {
    padding: 20px;
  }
}
</style>
